package org.leetcode.middle.leetcode3;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        String str = "au";
        int maxLength = solution.lengthOfLongestSubstring7(str);
        System.out.println(maxLength);
    }


    public int lengthOfLongestSubstring7(String s) {

        int n = s.length();
        if (n==0 || n==1){
            return n;
        }
        HashSet<Character> set = new HashSet<>();

        int maxLength = 1;

        int left,right;

        for ( left = 0; left < n; left++) {
            right=left;
            while (right<n && !set.contains(s.charAt(right))){
                set.add(s.charAt(right));
                right++;
            }
            maxLength=Math.max(maxLength,right-left);
            set.clear();
        }

//        for (int i = 0; i < n; i++) {
//            for (int j = i; j < n; j++) {
//                if (set.contains(s.charAt(j))){
//                    maxLength=Math.max(maxLength,j-i);
//                    break;
//                }else {
//                    set.add(s.charAt(j));
//                }
//            }
//            set.clear();
//        }

        return maxLength;
    }

    public int lengthOfLongestSubstring6(String s) {
        int n = s.length();

        if (n == 0 || n == 1) {
            return n;
        }

        int res = 0;
        int left, right;

        Set<Character> set = new HashSet<>();
        for (left = 0; left < n; left++) {
            right = left;

            while (right < n && !set.contains(s.charAt(right))) {
                set.add(s.charAt(right));
                right++;
            }
            set.clear();
            res = Math.max(res, right - left);
        }
        return res;
    }

    public int lengthOfLongestSubstring5(String s) {

        if (s.length() == 0 || s.length() == 1) {
            return s.length();
        }

        int res = 0;

        HashSet<Character> set = new HashSet<>();

        int n = s.length();

        int left, right;

        for (left = 0; left < n; left++) {

            right = left;

            while (right < n && !set.contains(s.charAt(right))) {
                set.add(s.charAt(right));
                right++;
            }
            res = Math.max(res, right - left);

            set.clear();
        }

        return res;
    }

    public int lengthOfLongestSubstring3(String s) {
        if (s.length() == 0) {
            return 0;
        }

        int left = 0;
        int maxLength = 0;

        HashMap<Character, Integer> map = new HashMap<>();

        for (int right = 0; right < s.length(); right++) {

            if (map.containsKey(s.charAt(right))) {
                left = Math.max(left, map.get(s.charAt(right)) + 1);
            }
            map.put(s.charAt(right), right);

            maxLength = Math.max(maxLength, right - left + 1);
        }

        return maxLength;
    }

    /**
     * 错误
     */
    public int lengthOfLongestSubstring2(String s) {

        Set<Character> set = new HashSet<>();


        int left = 0, right = 1;

        int res = 0;


        while (right < s.length()) {
            if (set.contains(s.charAt(left)) || s.charAt(left) == s.charAt(right)) {
                if (s.charAt(left) == s.charAt(right)) {
                    res = Math.max(res, set.size() + 1);
                } else {
                    res = Math.max(res, set.size());
                }
                set.clear();
                set.add(s.charAt(left));
            } else {
                set.add(s.charAt(left));
            }

            left++;
            right++;
        }
        res = Math.max(res, set.size());

        return res;
    }

    /**
     * 题目看错,方法不对
     */
    public int lengthOfLongestSubstring(String s) {

        if (s.length() < 2) {
            return s.length();
        }


        char[] charArray = s.toCharArray();

        int maxSize = 0;

        int slow = 0;
        int fast = 1;

        int length = 1;
        while (fast < s.length() - 1) {

            if (charArray[fast] - charArray[slow] == 1) {
                length++;
            } else {
                length = 1;
            }
            slow = fast;
            fast++;

            maxSize = Math.max(maxSize, length);
        }

        return maxSize;
    }



}
